#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

"${DIRECTORY}/api" apt_lock_wait

# Remove old MCPILs
sudo apt-get remove -y mcpil-r mcpil &>/dev/null || true

wget -qO- https://raw.githubusercontent.com/MCPI-Revival/mcpi-packages/master/install.sh | grep -v 'sudo apt update' | sudo bash - || error "Failed to install APT repo!"

# Choose MCPIL to use
PS3="Which launcher for MCPI Modded would you like to use? Need help deciding? Ask here: https://discord.com/invite/aDqejQGMMy
"
options=("gMCPIL (recommended)" "jMCPIL (better GUI)" "Quit")
select opt in "${options[@]}"
do
  case $opt in
    "${options[0]}")
        MCPIL="gmcpil"
        break;;
    "${options[1]}")
        MCPIL="jmcpil"
        break;;
    "${options[2]}")
        exit 0;;
    *) echo "Invalid option: $REPLY";;
  esac
done

"${DIRECTORY}/pkg-install" "$MCPIL python3-pip" "$(dirname "$0")" || error "Failed to install ${MCPIL} and python3-pip packages!"
pip3 install mcpi || error "The pip3 command failed to install mcpi!"
